Skip to content

perf: bypass ConcreteInstr validation in concrete_instructions#201

Merged
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/trusted-concreteinstr
May 22, 2026
Merged

perf: bypass ConcreteInstr validation in concrete_instructions#201
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/trusted-concreteinstr

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

Two changes to the hot encode loop in _ConvertBytecodeToConcrete.concrete_instructions:

  1. ConcreteInstr._from_trusted factory

    ConcreteInstr(instr_name, c_arg, location=location) at the bottom of the loop went through the full init -> _check_arg -> _set chain on every instruction, even though name/opcode/arg/location are all derived from already-validated Instr objects. A new _from_trusted classmethod bypasses _check_arg entirely and replicates only the size computation from _set, using object.new + direct slot assignment — the same pattern as the existing _from_opcode fast path.

  2. Direct private slot access

    instr.location, instr.name, and instr.arg are properties whose bodies are each a single "return self._xxx". Accessing instr._location, instr._name, and instr._arg directly avoids the property descriptor lookup on every iteration. This is safe because instr is a validated Instr instance at this point (guarded by the assert isinstance check above).

Profile data

Hotspot Before After
concrete_instructions own 8.07% 5.44%
concrete_instructions total 11.71% 9.44%
ConcreteInstr.__init__ (child) 1.30% total gone

Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each)

Avg Stddev
Before 223.7 r/s ±4.7
After 246.1 r/s ±3.3

+22.4 r/s / +10.0% throughput improvement.

Two changes to the hot encode loop in _ConvertBytecodeToConcrete.concrete_instructions:

1. ConcreteInstr._from_trusted factory

   ConcreteInstr(instr_name, c_arg, location=location) at the bottom of the
   loop went through the full __init__ -> _check_arg -> _set chain on every
   instruction, even though name/opcode/arg/location are all derived from
   already-validated Instr objects. A new _from_trusted classmethod bypasses
   _check_arg entirely and replicates only the size computation from _set,
   using object.__new__ + direct slot assignment — the same pattern as the
   existing _from_opcode fast path.

2. Direct private slot access

   instr.location, instr.name, and instr.arg are properties whose bodies are
   each a single "return self._xxx". Accessing instr._location, instr._name,
   and instr._arg directly avoids the property descriptor lookup on every
   iteration. This is safe because instr is a validated Instr instance at this
   point (guarded by the assert isinstance check above).

Profile data

| Hotspot | Before | After |
|---|---|---|
| concrete_instructions own | 8.07% | 5.44% |
| concrete_instructions total | 11.71% | 9.44% |
| ConcreteInstr.__init__ (child) | 1.30% total | gone |

Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each)

| | Avg | Stddev |
|---|---|---|
| Before | 223.7 r/s | ±4.7 |
| After | 246.1 r/s | ±3.3 |

+22.4 r/s / +10.0% throughput improvement.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.43%. Comparing base (bd74da9) to head (43a0c0e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #201      +/-   ##
==========================================
+ Coverage   95.40%   95.43%   +0.03%     
==========================================
  Files           7        7              
  Lines        2110     2126      +16     
  Branches      456      458       +2     
==========================================
+ Hits         2013     2029      +16     
  Misses         54       54              
  Partials       43       43              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@P403n1x87 P403n1x87 marked this pull request as ready for review May 22, 2026 14:04
@MatthieuDartiailh MatthieuDartiailh merged commit 41c679b into MatthieuDartiailh:main May 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants